<?php
/**
 *权限调用类
 *@author sulwan@126.com
 */

 class Role extends Application{
    public $Classlist;

 public function __construct() {
    parent::__construct();
 }


/**
 *获取角色列表
 */
public function getRole($page,$num,$findkey='',$order='id',$fields='*') {
	$numrows = intval($num);
	$page = (intval($page) > 0) ? intval($page) : 1;
	$offset = ($page -1) * $numrows;
	#
	$where = is_array($findkey) ? implode(' AND ', $findkey) : $findkey;
	$where = empty ($where) ? '' : ' WHERE ' . $where;
	#
	$orderby = ($orderby == '+') ? 'ASC' : 'DESC';
	$order = empty ($order) ? '' : (' ORDER BY ' . $order . ' ' . $orderby);
	#
	$limits =  '  LIMIT '.$offset.','.$numrows;
	#
	$sql = 'SELECT '. $fields . ' FROM ' . '#happy_role_sys';
	$sql .= $where . $order . $limits;
	#
	$rs = $this->_db->getAll($sql);
	return $rs;
}


/**
 *统计角色类别数量
 */
public function getRoleCount($findkey='') {
	$where = is_array($findkey) ? implode(' AND ', $findkey) : $findkey;
	$where = empty ($where) ? '' : ' WHERE ' . $where;
	$sql = 'SELECT count(*) FROM #happy_role_sys';
	$sql .= $where;
	return $this->_db->getOne($sql);
}

/**
 *添加角色
 */
public function sys_addRole($data) {
	$data = array_map('mysql_escape',$data);
	$sql = "INSERT INTO `#happy_role_sys`(`".implode("`,`",array_keys($data))."`)VALUES('".implode("','",array_values($data))."')";
	$id = $this->_db->execute($sql);
	return $id;
}


/**
 *查看有哪些模块
 */
public function roleTreeSort(){
	$data = getCache(MENU_TREE,'roleTree');
	if(empty($data)) {
		$data = array();
		$this->roleclassSort();
		$temp = $this->Classlist->getTreeList();
		if(empty($temp[0]->id)) {
			return $data;
		}
		foreach ($temp as $k => $v) {					//添加深度
			$data[] = $v->obj;
			$data[$k]['depth'] = $v->depth;
			$fields[] = 'pid = '.$data[$k]['id'];
			$fields[] = 'display = '.'\'none\'';
			$data[$k]['Children'] = $this->seachFields($fields);
			unset($fields);
			//$data[$k]['state'] = $data[$k]['Children']?'closed':'open';
		}
		setCache(MENU_TREE,'roleTree',$data);
	}
        return $data;
}


/**
 *检索是否存存在指定字段的指定值
 */
public function seachFields($fields) {
	$where = is_array($fields) ? implode(' AND ', $fields) : $fields;
	$where = empty ($where) ? '' : ' WHERE ' . $where;
    $sql = "SELECT * FROM #happy_role_column";
	$sql .= $where;
    $data = $this->_db->getOne($sql);
	if(!empty($data)) {
		return TRUE;
	}
	return FALSE;
}


/**
*获取现有模块
*/
public function roleModulegory() {
	$rs = getCache(MENU_TREE,'column');
	if(empty($rs)) {
		$sql = "SELECT * FROM #happy_role_column;";
		$rs = $this->_db->getAll($sql);
	}
	setCache(MENU_TREE,'column',$rs);
    return $rs;
}



/**
 *模块数据赋值
 */
public function roleclassSort() {
     $rsList = $this->roleModulegory();
     $this->Classlist = new CCategory();
     $this->Classlist->assign ($rsList, 'id', 'pid', 'taxis');
}



//查询角色数据
public function seachRole($id) {
	if(empty($id)) {
		return 0;
	}
	$role = getCache(MENU_GROUP,$id);
	if(empty($role)) {
		$roleSql = "SELECT * FROM `#happy_role_sys` WHERE id = ".$id;
		$role =  $this->_db->getRow($roleSql);
		if(!empty($role['role'])) {
			$role['role'] = explode("|", $role['role']);
		}else{
			$role['role'] = array();
		}
	}
	setCache(MENU_GROUP,$id,$role);
	return $role;
}




/**
 *保存修改角色
 */
 public function sys_SaveEditRole($data,$id) {
		$data = array_map('mysql_escape',$data);
		foreach ($data as $key => $value){
			  $sqlBody .= '`'.$key.'`'." = '".$value."',";
		}
		$sqlBody = substr($sqlBody,0,-1);
		$sql = "UPDATE `#happy_role_sys` SET ".$sqlBody." where   id =".$id." LIMIT 1";
		$data = $this->_db->execute($sql);
		delCache(MENU_GROUP,$id);
		delCache(MENU_TREE);
		return $data;
 }



/**
 *删除角色
 */
 public function sys_deltRole($id) {
		if(empty($id)) {
			return 0;
		}
		$sql = "DELETE FROM `#happy_role_sys` WHERE `id` = ".$id." LIMIT 1";
		$sign = $this->_db->execute($sql);
		delCache(MENU_GROUP,$id);
		delCache(MENU_TREE);
		return $id;
 }


/**
 *获取模块列表
 */
public function getModuleRole($page,$num,$findkey='',$order='id',$fields='*') {
	$numrows = intval($num);
	$page = (intval($page) > 0) ? intval($page) : 1;
	$offset = ($page -1) * $numrows;
	#
	$where = is_array($findkey) ? implode(' AND ', $findkey) : $findkey;
	$where = empty ($where) ? '' : ' WHERE ' . $where;
	#
	$orderby = ($orderby == '+') ? 'ASC' : 'DESC';
	$order = empty ($order) ? '' : (' ORDER BY ' . $order . ' ' . $orderby);
	#
	$limits =  '  LIMIT '.$offset.','.$numrows;
	#
	$sql = 'SELECT '. $fields . ' FROM ' . '#happy_role_column';
	$sql .= $where . $order . $limits;
	#
	$rs = $this->_db->getAll($sql);
	return $rs;
}


/**
 *统计模块数量
 */
public function getModuleRoleCount($findkey='') {
	$where = is_array($findkey) ? implode(' AND ', $findkey) : $findkey;
	$where = empty ($where) ? '' : ' WHERE ' . $where;
	$sql = 'SELECT count(*) FROM #happy_role_column';
	$sql .= $where;
	return $this->_db->getOne($sql);
}


/**
 *添加模块
 */
 public function addRoleModule($data) {
	 	$data = array_map('mysql_escape',$data);
		$sql = "INSERT INTO `#happy_role_column`(`".implode("`,`",array_keys($data))."`)VALUES('".implode("','",array_values($data))."')";
		$data = $this->_db->execute($sql);
		delCache(MENU_TREE);
		return $data;
 }


/**
 *所属模块:根据父节点获取全部子节点的全部信息
 */
function Rolefzid ($id) {
    $this->roleclassSort();
    $rs				=	$this->Classlist->getChildren($id);
    if (!empty($rs)){
        foreach($rs as $k=>$v)
        {
          $temp[$k]['name']				=	$v->obj['text'];
          $temp[$k]['mid']				=	$v->obj['mid'];
          $temp[$k]['id']				=	$v->id;
		  $temp[$k]['pid']				=	$v->obj['pid'];
		  $temp[$k]['binid']				=	$v->obj['binid'];
        }
    }
    return $temp;
}


 /**
  *删除模块
  */
  public function DelteModule($id) {
	  $sql = "DELETE FROM `#happy_role_column` WHERE `id` = ".$id." LIMIT 1";
	  $sign = $this->_db->execute($sql);
	  delCache(MENU_TREE);
	  return $sign;
  }



/**
 *查看模块
 */
 public function lookRoleModule($id) {
		$sql = "SELECT * FROM `#happy_role_column` WHERE id=".$id;
		$data = $this->_db->getRow($sql);
		return $data;
 }


/**
 *权限检查(登陆用后验证是否有操作权限)
 */
public function RoleX($filename,$action) {
		//获取权限ID
    	$userRoleSql =  "SELECT id  FROM `core_role_column` WHERE `file` = '".$filename."' AND `action` = '".$action."'";
		$roleId = $this->_db->getOne($userRoleSql);
		return $roleId;
}


/**
 *获取下属权限
 */
public function getRoleX($id) {
	$roleSql = "SELECT * FROM `#happy_role_column` WHERE pid = ".$id;
	return $this->_db->getAll($roleSql);
}



/**
 *保存修改分类
 */
 public function editSaveModule($data,$id) {
		if(empty($id)) {
			return 0;
		}
		$data = array_map('mysql_escape',$data);
		foreach ($data as $key => $value){
			  $sqlBody .= $key." = '".$value."',";
		}
		$sqlBody = substr($sqlBody,0,-1);
		$sql = "UPDATE `#happy_role_column` SET ".$sqlBody." where   id =".$id." LIMIT 1";
		$data = $this->_db->execute($sql);
        delCache(MENU_TREE);
 }



/**
 *设置分类排序
 */
public function taxisModulegory($value,$key) {
	if(empty($key)) {
		return 1;
	}
	$sql = " UPDATE `#happy_role_column` SET `taxis` = '".intval($value)."' WHERE `id` =".$key;
    delCache(MENU_TREE);
	return $this->_db->execute($sql);
}



/**
 *获取部门列表
 */
public function getDepartmentList($page,$num,$findkey='',$order='bid',$fields='*') {
	$numrows = intval($num);
	$page = (intval($page) > 0) ? intval($page) : 1;
	$offset = ($page -1) * $numrows;
	#
	$where = is_array($findkey) ? implode(' AND ', $findkey) : $findkey;
	$where = empty ($where) ? '' : ' WHERE ' . $where;
	#
	$orderby = ($orderby == '+') ? 'ASC' : 'DESC';
	$order = empty ($order) ? '' : (' ORDER BY ' . $order . ' ' . $orderby);
	#
	$limits =  '  LIMIT '.$offset.','.$numrows;
	#
	$sql = 'SELECT '. $fields . ' FROM ' . '#happy_department';
	$sql .= $where . $order . $limits;
	#
	$rs = $this->_db->getAll($sql);
	return $rs;
}



/**
 *保存部门
 */
 function saveDepartment($data) {
	$data = array_map('mysql_escape',$data);
	$sql = "INSERT INTO `#happy_department`(`".implode("`,`",array_keys($data))."`)VALUES('".implode("','",array_values($data))."')";
	$data = $this->_db->execute($sql);
	return $data;
 }



/**
 *获取机构列表
 */
public function getApparatusList($page,$num,$findkey='',$order='jid',$fields='*') {
	$numrows = intval($num);
	$page = (intval($page) > 0) ? intval($page) : 1;
	$offset = ($page -1) * $numrows;
	#
	$where = is_array($findkey) ? implode(' AND ', $findkey) : $findkey;
	$where = empty ($where) ? '' : ' WHERE ' . $where;
	#
	$orderby = ($orderby == '+') ? 'ASC' : 'DESC';
	$order = empty ($order) ? '' : (' ORDER BY ' . $order . ' ' . $orderby);
	#
	$limits =  '  LIMIT '.$offset.','.$numrows;
	#
	$sql = 'SELECT '. $fields . ' FROM ' . '#happy_apparatus';
	$sql .= $where . $order . $limits;
	#
	$rs = $this->_db->getAll($sql);
	return $rs;
}

/**
 *获取部门用户
 */
function departmentListUser($page,$num,$findkey='',$order='bid',$fields='*') {
	$numrows = intval($num);
	$page = (intval($page) > 0) ? intval($page) : 1;
	$offset = ($page -1) * $numrows;
	#
	$where = is_array($findkey) ? implode(' AND ', $findkey) : $findkey;
	$where = empty ($where) ? '' : ' WHERE ' . $where;
	#
	$orderby = ($orderby == '+') ? 'ASC' : 'DESC';
	$order = empty ($order) ? '' : (' ORDER BY ' . $order . ' ' . $orderby);
	#
	$limits =  '  LIMIT '.$offset.','.$numrows;
	#
	$sql = 'SELECT ' . $fields . ' FROM ' . '#happy_department';
	$sql .= $where . $order . $limits;
	#
	$rs = $this->_db->getAll($sql);
	return $rs;
}

/**
 *获取机构信息
 */
 function getApparatusRow($findkey,$fields='*') {
	$where = is_array($findkey) ? implode(' AND ', $findkey) : $findkey;
	$where = empty ($where) ? '' : ' WHERE ' . $where;
	$sql = 'SELECT '. $fields . ' FROM ' . '#happy_apparatus';
	$sql .= $where;
	$rs = $this->_db->getRow($sql);
	return $rs;
 }


 /**
  *删除部门
  */
  function deleteDepartment($bid) {
	  $sql = "DELETE FROM `#happy_department` WHERE `bid` = ".$bid." LIMIT 1";
	  $this->_db->execute($sql);
  }

 /**
  *获取部门信息
  */
  function getDepartmentRow($findkey,$fields='*') {
	  $where = is_array($findkey)?implode('AND ',$findkey) : $findkey;
	  $where = empty ($where) ? '' : ' WHERE ' . $where;
	  $sql = 'SELECT '. $fields . ' FROM ' . '#happy_department';
	  $sql .= $where;
	  $rs = $this->_db->getRow($sql);
	  return $rs;
  }
  /**
   *保存修改部门
   */
  public function saveEditDepartment($data,$bid) {
		if(empty($bid)) {
			return 0;
		}
		$data = array_map('mysql_escape',$data);
		foreach ($data as $key => $value){
			  $sqlBody .= $key." = '".$value."',";
		}
		$sqlBody = substr($sqlBody,0,-1);
		$sql = "UPDATE `#happy_department` SET ".$sqlBody." where   bid =".$bid." LIMIT 1";
		$data = $this->_db->execute($sql);
 }

 /**
  *删除机构
  */
  function deleteApparatus($jid) {
	  $sql = "DELETE FROM `#happy_apparatus` WHERE `jid` = ".$jid." LIMIT 1";
	  $this->_db->execute($sql);
  }

/**
 *保存机构
 */
 function saveApparatus($data) {
	$data = array_map('mysql_escape',$data);
	$sql = "INSERT INTO `#happy_apparatus`(`".implode("`,`",array_keys($data))."`)VALUES('".implode("','",array_values($data))."')";
	$data = $this->_db->execute($sql);
	return $data;
 }

  /**
   *保存修改机构
   */
  public function saveEditApparatus($data,$jid) {
		if(empty($jid)) {
			return 0;
		}
		$data = array_map('mysql_escape',$data);
		foreach ($data as $key => $value){
			  $sqlBody .= $key." = '".$value."',";
		}
		$sqlBody = substr($sqlBody,0,-1);
		$sql = "UPDATE `#happy_apparatus` SET ".$sqlBody." where   jid =".$jid." LIMIT 1";
		$data = $this->_db->execute($sql);
 }
}
 ?>